/*
 * Copyright 2009 Jiwei Xu (http://www.xujiwei.com)
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

provide("Tools.HandTool");

require("Tools.ToolBase");
require("Undo.MoveCommand");
require("Undo.BatchCommand");

MoveTool = Class(ToolBase, (function() {
    var isHitObject = false,
        isHitHandle = false,
        selectedObj = null,
        selectedHandle = -1,
        startX = 0, startY = 0,
        endX = 0, endY = 0;

    return {
        OnLButtonDown : function(view, e) {
            var doc = view.GetDocument();

            selectedObj = null;
            selectedHandle = -1;
            isHitObject = false;
            isHitHandle = false;
            startX = e.X;
            startY = e.Y;

            var selobjs = doc.GetSelectedObjects();
            if (selobjs.length > 0) {
                selectedObj = selobjs;
                for (var i = 0; i < selobjs.length; ++i) {
                    var hi = selobjs[i].HitHandleTest(e);
                    if (hi != -1) {
                        selectedObj = selobjs[i];
                        selectedHandle = hi;
                        //isHitObject = true;
                        isHitHandle = true;
                        break;
                    }
                }
            } 
            
            if (!isHitHandle) {
                var obj = doc.ObjectAt(e);
                if (obj) {
                    doc.Select(obj);
                    selectedObj = [obj];
                    TinyMQ.Publish("canvashop.object.selectedone", selectedObj);
                    view.SetCursor("move");

                    isHitObject = true;
                } else {
                    TinyMQ.Publish("canvashop.object.selectednone");
                    doc.SelectNone();
                }
                view.Update();
           }
        },

        OnMouseMove : function(view, e) {
            var delta = {
                X : e.X - endX,
                Y : e.Y - endY
            };
            endX = e.X;
            endY = e.Y;

            if (isHitHandle) {
                selectedObj.MoveHandle(selectedHandle, delta);
                view.Update();
            } else if (isHitObject) {
                for (var i = 0; i < selectedObj.length; ++i) {
                    selectedObj[i].Move(delta);
                    view.Update();
                }
            }
        },

        OnLButtonUp : function(view, e) {
            if (isHitHandle || isHitObject) {
                var cmd = new MoveCommand(selectedObj, {
                    IsMovedHandle : isHitHandle,
                    HandleIndex : selectedHandle,
                    Offset : {
                        X : endX - startX,
                        Y : endY - startY
                    }
                });
                view.UndoManager.Push(cmd);
            }

            isHitObject = false;
            isHitHandle = false;
            view.ResetCursor();
        }
    };
})());

new MoveTool();